package com.ztx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztx.pojo.Permission;
import com.ztx.pojo.Role;
import com.ztx.pojo.RolePermission;
import com.ztx.pojo.dto.RoleDto;
import com.ztx.service.PermissionService;
import com.ztx.service.RolePermissionService;
import com.ztx.service.RoleService;
import com.ztx.mapper.RoleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ztx
 * @description 针对表【role】的数据库操作Service实现
 * @createDate 2023-10-27 13:44:34
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
        implements RoleService {

    @Autowired
    RolePermissionService rolePermissionService;

    @Autowired
    PermissionService permissionService;

    @Transactional
    @Override
    public void saveWithPermissions(RoleDto roleDto) {
        this.save(roleDto);

        List<Permission> permissions = roleDto.getPermissions();
        List<RolePermission> rolePermissions = new ArrayList<>(permissions.size());
        permissions.forEach(permission -> {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleDto.getRoleId());
            rolePermission.setPermissionId(permission.getPermissionId());
            rolePermissions.add(rolePermission);
        });
        rolePermissionService.saveBatch(rolePermissions);
    }

    @Override
    public List<RoleDto> listRoleDto() {
        List<Role> roles = this.list();
        List<RoleDto> roleDtos = new ArrayList<>(roles.size());
        roles.forEach(role -> {
            RoleDto roleDto = new RoleDto();
            BeanUtils.copyProperties(role, roleDto);

            // Permissions
            LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(RolePermission::getRoleId, role.getRoleId());
            List<RolePermission> rolePermissions = rolePermissionService.list(queryWrapper);
            if (!rolePermissions.isEmpty()) {
                List<Long> permissionIds = new ArrayList<>(rolePermissions.size());
                rolePermissions.forEach(rolePermission -> {
                    permissionIds.add(rolePermission.getPermissionId());
                });
                LambdaQueryWrapper<Permission> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.in(Permission::getPermissionId, permissionIds);
                List<Permission> permissions = permissionService.list(queryWrapper1);
                roleDto.setPermissions(permissions);
            }

            roleDtos.add(roleDto);
        });
        return roleDtos;
    }

    @Transactional
    @Override
    public void deleteWithPermissions(Long id) {
        this.removeById(id);
        LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RolePermission::getRoleId, id);
        rolePermissionService.remove(queryWrapper);
    }

    @Override
    public RoleDto getByIdWithPermissions(Long roleId) {

        System.out.println("iid:"+roleId);

        RoleDto roleDto = new RoleDto();
        Role role = this.getById(roleId);
        BeanUtils.copyProperties(role, roleDto);

        LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RolePermission::getRoleId, roleId);
        List<RolePermission> rolePermissions = rolePermissionService.list(queryWrapper);

        System.out.println(rolePermissions.size()+" size 的数量");

        if (!rolePermissions.isEmpty()) {
            List<Long> permissionIds = new ArrayList<>(rolePermissions.size());
            rolePermissions.forEach(rolePermission -> {
                permissionIds.add(rolePermission.getPermissionId());
            });
            LambdaQueryWrapper<Permission> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.in(Permission::getPermissionId, permissionIds);
            List<Permission> permissions = permissionService.list(queryWrapper1);
            roleDto.setPermissions(permissions);
        }

        return roleDto;
    }

    @Override
    public void updateWithPermissions(RoleDto roleDto) {
        this.updateById(roleDto);
        LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RolePermission::getRoleId,roleDto.getRoleId());
        rolePermissionService.remove(queryWrapper);

        List<Permission> permissions = roleDto.getPermissions();
        List<RolePermission> rolePermissions = new ArrayList<>(permissions.size());
        permissions.forEach(permission -> {
            RolePermission rolePermission = new RolePermission();

            rolePermission.setPermissionId(permission.getPermissionId());
            rolePermission.setRoleId(roleDto.getRoleId());

            rolePermissions.add(rolePermission);
        });
        rolePermissionService.saveBatch(rolePermissions);
    }
}




